package com.jumi.microservice.controller.api;

import cn.hutool.core.util.ObjectUtil;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.jwt.annon.IgnoreUrlsAnnon;
import com.jm.member.common.jwt.constant.JwtAdminInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.entity.JmNotice;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.service.IJmNoticeService;
import com.jumi.microservice.service.IJmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/JmUserMsg")
@Api(tags = "C-消息通知")
public class JmUserMsgController {
    @Autowired
    IJmUserService userService;

    @Resource
    IJmNoticeService jmNoticeService;

    @GetMapping("/userMsg")
    @ApiOperation(value = "用户消息通知")
    public ResponseResult userMsg() {
        Integer userId = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        JmUser user = userService.getById(userId);
        if (ObjectUtil.isNotEmpty(user)) {
            return ResponseResult.success(user);
        }
        return ResponseResult.error();
    }

    @PostMapping("/addNoticeTimes")
    @ApiOperation(value = "新增用户读取推送次数")
    @ApiImplicitParam(name = "messageId", value = "消息ID")
    @Transactional
    @IgnoreUrlsAnnon("/addNoticeTimes")
    public ResponseResult addNoticeTimes(Integer messageId) {
        try {
            JmNotice jmNotice = jmNoticeService.getById(messageId);
            jmNotice.setReadNum(jmNotice.getReadNum() + 1);
            Boolean rs = jmNoticeService.updateById(jmNotice);
            return ResponseResult.success(rs);
        } catch (Exception e) {
            return ResponseResult.error("新增阅读次数异常,请联系管理员");
        }
    }
}
